package com.iplant.mes.serviceImpl;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.cache.RedisUtils;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.QMSService;
import com.iplant.mes.service.po.qms.QMSMaterialCheck;
import com.iplant.mes.service.po.qms.QMSPassRate;
import com.iplant.mes.service.po.qms.QMSPassStationRate;
import com.iplant.mes.serviceImpl.dao.qms.QMSMaterialCheckDAO;
import com.iplant.mes.serviceImpl.dao.sfc.SFCTaskReportDAO;
import com.iplant.mes.serviceImpl.dao.sfc.SFCWorkpieceRecordDAO;
import com.iplant.wms.service.po.batch.MSSBatchRange;
import com.iplant.wms.service.po.batch.MSSBatchRecord;
import com.iplant.wms.serviceImpl.dao.batch.MSSBatchRangeDAO;
import com.iplant.wms.serviceImpl.dao.batch.MSSBatchRecordDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Slf4j
@Service
public class QMSServiceImpl implements QMSService {

    //region 构造函数
    public QMSServiceImpl() {
    }
    //endregion

    //region 单实例

    private static QMSService Instance = null;

    public static QMSService getInstance() {
        if (Instance == null)
            Instance = new QMSServiceImpl();

        return Instance;
    }

    //endregion

    @Autowired
    RedisUtils mRedisUtils;
    @Override
    public ServiceResult<List<QMSPassStationRate>> QMS_GetPassStationRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, int wStatType, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassStationRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCTaskReportDAO.getInstance().QMS_GetPassStationRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassStationRate>> QMS_GetStationPassStationRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, int wStatType, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassStationRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCTaskReportDAO.getInstance().QMS_GetStationPassStationRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassStationRate>> QMS_GetPassStationRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassStationRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCTaskReportDAO.getInstance().QMS_GetPassStationRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassStationRate>> QMS_GetStationPassStationRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassStationRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCTaskReportDAO.getInstance().QMS_GetStationPassStationRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassRate>> QMS_GetPassRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, int wStatType, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCWorkpieceRecordDAO.getInstance().QMS_GetPassRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassRate>> QMS_GetProductPassRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, int wStatType, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCWorkpieceRecordDAO.getInstance().QMS_GetProductPassRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassRate>> QMS_GetPassRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCWorkpieceRecordDAO.getInstance().QMS_GetPassRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSPassRate>> QMS_GetProductPassRateList(BMSEmployee wLoginUser, int wProductID, int wOrderID, String wOrderNo, Calendar wStartDate, Calendar wEndDate) {
        ServiceResult<List<QMSPassRate>> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = SFCWorkpieceRecordDAO.getInstance().QMS_GetProductPassRateList(wLoginUser, wProductID, wOrderID, wOrderNo, wStartDate, wEndDate, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<MSSBatchRecord> QMS_CreateBatchRecordByMaterialCheck(BMSEmployee wLoginUser, QMSMaterialCheck wMaterialCheck) {


        ServiceResult<MSSBatchRecord> wResult = new ServiceResult<>();
        try {


            MSSBatchRecord wBatchRecord = new MSSBatchRecord();
            wResult.Result = wBatchRecord;
            if (wMaterialCheck == null) {
                wResult.FaultCode += RetCode.SERVER_RST_ERROR_OUT;
                return wResult;
            }
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (StringUtils.isEmpty(wMaterialCheck.BatchRecordCode)) {

                MSSBatchRange wMSSBatchRange = MSSBatchRangeDAO.getInstance().MSS_QueryBatchRange(
                        wLoginUser, wMaterialCheck.SupplierID, wMaterialCheck.MaterialCode, wErrorCode);
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }

                if (wMSSBatchRange == null || wMSSBatchRange.ID <= 0
                        || StringUtils.isEmpty(wMSSBatchRange.RuleCode)) {
                    wResult.FaultCode += "无批次规则，无法生成批次记录";
                    return wResult;
                }
                wMaterialCheck.BatchRuleCode = wMSSBatchRange.RuleCode;
            }


            wBatchRecord.ID = wMaterialCheck.BatchRecordID;
            wBatchRecord.Code = wMaterialCheck.BatchRecordCode;
            wBatchRecord.ProductDate = wMaterialCheck.ProductDate;
            wBatchRecord.ExpireDate = wMaterialCheck.ExpireDate;
            wBatchRecord.StockDate = wMaterialCheck.StockDate;


            wBatchRecord.CustomerID = wMaterialCheck.SupplierID;
            wBatchRecord.CustomerCode = wMaterialCheck.SupplierCode;
            wBatchRecord.CustomerName = wMaterialCheck.SupplierName;

            wBatchRecord.MaterialID = wMaterialCheck.MaterialID;
            wBatchRecord.MaterialNo = wMaterialCheck.MaterialCode;
            wBatchRecord.MaterialName = wMaterialCheck.MaterialName;

            wBatchRecord.RuleCode = wMaterialCheck.BatchRuleCode;

            if (wBatchRecord.ProductDate == null) {
                wBatchRecord.ProductDate = Calendar.getInstance();
            }
            if (wBatchRecord.ExpireDate == null) {
                wBatchRecord.ExpireDate = CalendarTool.AddCalendar(Calendar.getInstance(), Calendar.YEAR, 10);
            }
            if (wBatchRecord.StockDate == null) {
                wBatchRecord.StockDate = Calendar.getInstance();
            }
            MSSBatchRecordDAO.getInstance().UpdatePo(wLoginUser, wBatchRecord, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            wMaterialCheck.BatchRecordID = wBatchRecord.ID;
            wMaterialCheck.BatchRecordCode = wBatchRecord.Code;
        }catch (Exception ex){
            log.error("QMS_CreateBatchRecordByMaterialCheck",ex);
            wResult.FaultCode+=ex.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> QMS_DeleteMaterialCheck(BMSEmployee wLoginUser, QMSMaterialCheck wMaterialCheck) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);

        if (wMaterialCheck == null) {
            wResult.FaultCode += RetCode.SERVER_RST_ERROR_OUT;
            return wResult;
        }
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        QMSMaterialCheckDAO.getInstance().DeletePO(wLoginUser, wMaterialCheck, wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }

        if (wMaterialCheck.BatchRecordID > 0) {
            MSSBatchRecordDAO.getInstance().MSS_DeleteByID(wLoginUser, wMaterialCheck.BatchRecordID, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
        }

        return wResult;
    }


}
